package com.cgmnx.admin.service.rbac.impl;

import com.cgmnx.admin.common.bean.Limiter;
import com.cgmnx.admin.common.exception.ServiceException;
import com.cgmnx.admin.dao.entity.rbac.RoleEntity;
import com.cgmnx.admin.dao.entity.rbac.RoleUserRelationEntity;
import com.cgmnx.admin.dao.mapper.rbac.RoleUserRelationMapper;
import com.cgmnx.admin.service.rbac.RoleUserRelationService;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class RoleUserRelationServiceImpl implements RoleUserRelationService {

//    @Autowired
    private RoleUserRelationMapper roleUserMappingMapper;

    @Override
    public List<RoleUserRelationEntity> queryByUserIdList(List<String> userIdList) {
        return roleUserMappingMapper.queryByUserIdList(userIdList);
    }

    @Override
    public void save(RoleUserRelationEntity entity) {
        roleUserMappingMapper.save(entity);
    }

    @Override
    public void saveAll(final List<Integer> roleIdList, final String userId, final String currentUser) {
        // 这里配置异常为了让biz层事务回滚
        if (CollectionUtils.isEmpty(roleIdList) || StringUtils.isBlank(userId)) {
            throw new ServiceException("用户名或者角色ID为空");
        }

        List<RoleUserRelationEntity> roleUserRelationEntities = FluentIterable.from(roleIdList).transform(new Function<Integer, RoleUserRelationEntity>() {
            @Override
            public RoleUserRelationEntity apply(Integer input) {

                if (input == null) {
                    return null;
                }

                RoleUserRelationEntity entity = new RoleUserRelationEntity();
                entity.setRoleId(input);
                entity.setUserId(userId);
                entity.setCreateUser(currentUser);
                entity.setUpdateUser(currentUser);
                return entity;
            }
        }).toList();

        roleUserMappingMapper.saveAll(roleUserRelationEntities);

    }

    @Override
    public Integer deleteByUserIdList(List<String> userIdList) {
        return roleUserMappingMapper.deleteByUserId(userIdList);
    }


    @Override
    public List<RoleUserRelationEntity> queryByRoleId(Integer id) {
        return roleUserMappingMapper.queryByRoleId(id);
    }

    @Override
    public Integer count(Integer id) {
        return roleUserMappingMapper.count(id);
    }

    @Override
    public List<RoleEntity> queryPage(Integer id, Limiter limiter) {
        return roleUserMappingMapper.queryPage(id, limiter);
    }

    @Override
    public int saveAll(List<RoleUserRelationEntity> list) {
        return roleUserMappingMapper.saveAll(list);
    }

    @Override
    public void deleteByUserIdListAndRoleId(Integer roleId, List<String> userIds) {
        roleUserMappingMapper.deleteByUserIdListAndRoleId(roleId, userIds);
    }
}
